package jp.co.withone.gadget.putiputi.base;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

/**
 * アプリケーションのベースとなるクラス
 * 
 * @author Sasaki Katsuyuki
 *
 */
public class PutiBody extends JFrame{

	JFrame frame;
	Container contentPane;

	JDialog dialog;

	protected static List<Frame> frames = new ArrayList<Frame>();
	
	public static Dimension dimension;
	
	JButton button1 = new JButton("○");
	JButton button2 = new JButton("○");
	JButton button3 = new JButton("○");
	JButton button4 = new JButton("○");
	JButton button5 = new JButton("○");
	JButton button6 = new JButton("○");
	JButton button7 = new JButton("○");
	JButton button8 = new JButton("○");
	JButton button9 = new JButton("○");

	/**
	 * コンストラクタ
	 * 
	 */
	public PutiBody() {

		frame = new JFrame("PutiPuti");
		contentPane = frame.getContentPane();
		frames.add(frame);
	}
	
	/**
	 * 起動用メソッド
	 * 
	 */
	public void run() {

		frame.addWindowListener(new WindowAdapter(){
			
			public void windowClosing(WindowEvent e) {
				showDialog();
			}
		});
		
		dimension = Toolkit.getDefaultToolkit().getScreenSize();
		
		button1.addActionListener(new PutiActionListener());
		button2.addActionListener(new PutiActionListener());
		button3.addActionListener(new PutiActionListener());
		button4.addActionListener(new PutiActionListener());
		button5.addActionListener(new PutiActionListener());
		button6.addActionListener(new PutiActionListener());
		button7.addActionListener(new PutiActionListener());
		button8.addActionListener(new PutiActionListener());
		button9.addActionListener(new PutiActionListener());
		
		JPanel panelUp = new JPanel();
		
		panelUp.setLayout(new GridLayout(3, 3));
		panelUp.add(button1);
		panelUp.add(button2);
		panelUp.add(button3);
		panelUp.add(button4);
		panelUp.add(button5);
		panelUp.add(button6);
		panelUp.add(button7);
		panelUp.add(button8);
		panelUp.add(button9);
		
		contentPane.add(panelUp);
		
		frame.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		frame.setSize(175,150);
		frame.setResizable(false);
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
	}
	
	/**
	 * ×ボタン押下時にダイアログを表示するメソッド
	 * 
	 */
	private void showDialog(){
		
		dialog = new JDialog(this,"Impossible",true);
		
		JLabel diaLabel = new JLabel("Nothing can stop me!", SwingConstants.CENTER);
		dialog.getContentPane().add(diaLabel);

		dialog.setSize(120,100);
		dialog.setResizable(false);
		dialog.setLocationRelativeTo(this.frame);
//		dialog.setModal(true);
//		setVisible(true);
		dialog.show();
	}

	/**
	 * 終了用メソッド
	 * 
	 */
	public void clear(){
		
		for (int i = 0; i < frames.size(); i++) {
			 
			frames.get(i).dispose();			
		}
		
		if(dialog != null){
			
			dialog.dispose();
		}
	}
	
}
